package main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import coreEngine.Communicator;
import coreEngine.GlobalVariables;


public class MainGUI {

	private JFrame satelliteSelectFrame;
//	private Container container;
//	private JComboBox satelliteComboBox;
	private SatelliteTracker tracker;
	//	private SatelliteTracker_Old tracker;
//	private JButton chooseButton;
	private Communicator comm;
	private String constellationName, constellationURL;
//	private JPanel panelRadioSatelliteButton;
//	private JRadioButton leoSatelliteType;
//	private JRadioButton meoSatelliteType;

//	private Font font = (new Font("Helvetica", Font.PLAIN, 11));

//	private Image icon = new ImageIcon("others/Logo.png").getImage();

	public MainGUI() {
		try {
			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (Exception e) {
			try {
				UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
			} catch (Exception ex) {
				
			}
		}

	}


	public Communicator getComm() {
		return comm;
	}

	public boolean checkConnection(){
		HttpURLConnection connection = null;
		int code=0;
		try {
			URL u = new URL("http://www.celestrak.com/NORAD/elements/");
			connection = (HttpURLConnection) u.openConnection();
			connection.setRequestMethod("HEAD");
			code = connection.getResponseCode();
			// You can determine on HTTP return code received. 200 is success.
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			return false;
		} finally {
			if (connection != null) {
				connection.disconnect();
			}
		}
		if(code == 200){
			return true;
		}
		return false;
	}

	public void startApp(){
		if(checkConnection()){
			GlobalVariables.internetConnectionAvailable = true;
			comm = new Communicator();
				
			constellationName = GlobalVariables.constellationName;
			comm.retrieveDataFromURL(constellationName,GlobalVariables.link);
			tracker = new SatelliteTracker(constellationName, MainGUI.this);
			tracker.init();
		}
		else{
			JOptionPane.showMessageDialog(satelliteSelectFrame, "No Internet connection. Not possible to update data!!!", "Error", JOptionPane.ERROR_MESSAGE);
			comm = new Communicator();		
			
			constellationName = GlobalVariables.constellationName;
			comm.retrieveData(constellationName);
			tracker = new SatelliteTracker(constellationName, MainGUI.this);
			tracker.init();
		}
	}


}




